package com.sunyur.tailor.channel.service.middle.org.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.dal.org.entity.OrganizationDO;
import com.sunyur.tailor.channel.service.inner.org.param.OrgSyncInfoParam;
import com.sunyur.tailor.channel.service.inner.org.service.SunyurOrgService;
import com.sunyur.tailor.channel.service.middle.org.MiddleOrgService;
import com.sunyur.tailor.channel.service.outer.org.model.OuterOrgModel;
import com.sunyur.tailor.channel.service.outer.org.param.OuterOrgParam;
import com.sunyur.tailor.channel.service.outer.org.service.OuterOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @Author 99288
 * @Description 组织业务实现类
 * @Date 23:17 2021/7/7
 * @Param
 * @return
**/
@Slf4j
@Service("middleOrgService")
public class MiddleOrgServiceImpl implements MiddleOrgService {

    @Autowired
    private OuterOrgService outerOrgService;
    @Autowired
    private SunyurOrgService sunyurOrgService;
//    @Autowired
//    private OrganizationService organizationService;

    @Override
    public ChannelResult syncOrg(OuterOrgParam outerOrgParam){
        //调用外部获取数据
        ChannelListResult<OuterOrgModel> outerOrgResult = outerOrgService.getOrgList(outerOrgParam);
        if(ChannelResultUtil.isSuccess(outerOrgResult)) {
            List<OuterOrgModel> outerOrgList = outerOrgResult.getContent();
            //数据转换
            List<OrgSyncInfoParam> orgSyncInfoParamList = this.conversion(outerOrgList);
            //调用内部同步数据
            sunyurOrgService.syncOrgList(orgSyncInfoParamList);
            //数据库入库
            List<OrganizationDO> organizationDOS = JSON.parseArray(JSON.toJSONString(outerOrgList), OrganizationDO.class);
//            organizationService.saveOrUpdateBatch(organizationDOS);
        }
        return ChannelResultUtil.success(Boolean.TRUE);
    }

    /**
     * 数据实体类转换
     */
    private List<OrgSyncInfoParam> conversion(List<OuterOrgModel> outerOrgList){
        List<OrgSyncInfoParam> orgSyncInfoParamList = new ArrayList<>();

        return orgSyncInfoParamList;
    }
}
